using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
using TapTap.Bootstrap;
using TapTap.Login;
using UnityEngine.Networking;
using System;
using System.Threading.Tasks;

public class SelectMode : MonoBehaviour
{
    public Button m_BtnJunior;
    public Button m_BtnSenior;
    public Button m_BtnExpert;
    public Button m_BtnCustomer;
    public CustomerPanel m_CustomerPanel;
    public GameMode m_GameMode;
    public Text m_TxtVersion;
    public Button m_BtnRecord;
    public RecordPanel m_RecordPanel;
    public RecordManager m_RecordManager;
    public Button m_BtnLogout;
    public RawImage m_ImgAvatar;
    public Text m_TxtNickName;

    //=========================================公私分界线==================================================
    void Awake()
    {
        m_BtnJunior.onClick.AddListener(OnBtnJuniorClick);
        m_BtnSenior.onClick.AddListener(OnBtnSeniorClick);
        m_BtnExpert.onClick.AddListener(OnBtnExpertClick);
        m_BtnCustomer.onClick.AddListener(OnBtnCustomerClick);
        m_BtnRecord.onClick.AddListener(OnBtnRecordClick);
        m_BtnLogout.onClick.AddListener(OnBtnLogoutClick);

        if (GameMode.Instance == null)
        {
            GameObject.Instantiate<GameObject>(m_GameMode.gameObject);
        }

        if (RecordManager.Instance == null)
        {
            GameObject.Instantiate<GameObject>(m_RecordManager.gameObject);
        }

        UpdateProfile();
    }

    void Start()
    {
        m_TxtVersion.text = string.Format("Version:{0}", UnityEngine.Application.version);
    }

    void OnBtnJuniorClick()
    {
        GameMode.Instance.Init(EGameMode.Junior, 9, 9, 10);
        SceneManager.LoadSceneAsync("GamePlay", LoadSceneMode.Single);
    }

    void OnBtnSeniorClick()
    {
        GameMode.Instance.Init(EGameMode.Senior, 16, 16, 40);
        SceneManager.LoadSceneAsync("GamePlay", LoadSceneMode.Single);
    }

    void OnBtnExpertClick()
    {
        GameMode.Instance.Init(EGameMode.Expert, 16, 30, 99);
        SceneManager.LoadSceneAsync("GamePlay", LoadSceneMode.Single);
    }

    void OnBtnCustomerClick()
    {
        m_CustomerPanel.Show();
    }

    void OnBtnRecordClick()
    {
        m_RecordPanel.Show();
    }

    async void OnBtnLogoutClick()
    {
        await LoginManager.Instance.Logout();
        SceneManager.LoadSceneAsync("Login", LoadSceneMode.Single);
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            OnNavigationBack();
        }
    }

    void OnNavigationBack()
    {
        if (m_CustomerPanel.gameObject.activeSelf)
        {
            m_CustomerPanel.Hide();
            return;
        }

        if (m_RecordPanel.gameObject.activeSelf)
        {
            m_RecordPanel.Hide();
            return;
        }

        OnBtnLogoutClick();
    }

    async void UpdateProfile()
    {
        bool isLogin = await LoginManager.Instance.IsLogin();
        bool isAnonymous = await LoginManager.Instance.IsAnonymous();
        if (isLogin && isAnonymous == false)
        {
            var profile = await TapLogin.FetchProfile();
            m_TxtNickName.text = profile.name;

            StopAllCoroutines();
            StartCoroutine(GetAvatar(profile.avatar, (texture) =>
            {
                m_ImgAvatar.texture = texture;
            }));

        }
        else
        {
            m_TxtNickName.text = "游客";
        }

    }

    IEnumerator GetAvatar(string url, Action<Texture> callback)
    {
        UnityWebRequest unityWebRequest = new UnityWebRequest(url);
        DownloadHandlerTexture downloadHandlerTexture = new DownloadHandlerTexture(true);
        unityWebRequest.downloadHandler = downloadHandlerTexture;
        yield return unityWebRequest.SendWebRequest();
        callback(downloadHandlerTexture.texture);
    }
}
